﻿using System;



namespace CircleGeometry

{

    // 圆类

    class Circle

    {

        // 私有字段

        private double radius;



        // 常量

        private const double PI = Math.PI;



        // 属性

        public double Radius

        {

            get { return radius; }

            set

            {

                if (value >= 0)

                    radius = value;

                else

                    throw new ArgumentException("半径不能为负数");

            }

        }



        // 无参构造方法（默认半径为1）

        public Circle()

        {

            radius = 1.0;

        }



        // 带参构造方法

        public Circle(double radius)

        {

            Radius = radius; // 使用属性设置，确保验证逻辑

        }



        // 计算周长

        public double CalculateCircumference()

        {

            return 2 * PI * radius;

        }



        // 计算面积

        public double CalculateArea()

        {

            return PI * radius * radius;

        }



        // 显示圆的信息

        public void DisplayInfo()

        {

            Console.WriteLine($"圆: 半径 = {radius}");

            Console.WriteLine($"周长 = {CalculateCircumference():F4}");

            Console.WriteLine($"面积 = {CalculateArea():F4}");

            Console.WriteLine();

        }



        // 静态方法：通过直径创建圆

        public static Circle FromDiameter(double diameter)

        {

            if (diameter < 0)

                throw new ArgumentException("直径不能为负数");



            return new Circle(diameter / 2);

        }

    }



    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("圆类测试 - 周长与面积计算");

            Console.WriteLine("==========================");

            Console.WriteLine();



            try

            {

                // 测试默认构造方法

                Circle circle1 = new Circle();

                Console.WriteLine("默认圆 (半径=1.0):");

                circle1.DisplayInfo();



                // 测试带参构造方法

                Circle circle2 = new Circle(5.0);

                Console.WriteLine("半径为5.0的圆:");

                circle2.DisplayInfo();



                // 测试浮点数半径

                Circle circle3 = new Circle(2.5);

                Console.WriteLine("半径为2.5的圆:");

                circle3.DisplayInfo();



                // 测试通过直径创建圆

                Circle circle4 = Circle.FromDiameter(10.0);

                Console.WriteLine("通过直径10.0创建的圆:");

                circle4.DisplayInfo();



                // 测试属性设置

                Circle circle5 = new Circle();

                circle5.Radius = 3.0;

                Console.WriteLine("通过属性设置半径为3.0的圆:");

                circle5.DisplayInfo();



                // 测试边界情况

                Circle circle6 = new Circle(0);

                Console.WriteLine("半径为0的圆:");

                circle6.DisplayInfo();



                // 测试异常情况（负数半径）

                try

                {

                    Circle invalidCircle = new Circle(-2.0);

                    Console.WriteLine("半径为-2.0的圆:");

                    invalidCircle.DisplayInfo();

                }

                catch (ArgumentException ex)

                {

                    Console.WriteLine($"错误: {ex.Message}");

                    Console.WriteLine();

                }



                // 计算多个圆的面积总和

                Circle[] circles = { circle1, circle2, circle3, circle4, circle5 };

                double totalArea = 0;



                foreach (Circle circle in circles)

                {

                    totalArea += circle.CalculateArea();

                }



                Console.WriteLine($"所有圆的面积总和: {totalArea:F4}");

                Console.WriteLine();



                // 用户交互测试

                Console.WriteLine("用户交互测试:");

                Console.Write("请输入圆的半径: ");

                double userRadius = Convert.ToDouble(Console.ReadLine());



                Circle userCircle = new Circle(userRadius);

                userCircle.DisplayInfo();

            }

            catch (Exception ex)

            {

                Console.WriteLine($"发生错误: {ex.Message}");

            }



            Console.ReadLine();

        }

    }

}